package com.ruicar.afs.cloud.bizcommon.commission.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.bizcommon.commission.dto.basecommission.AttachbacklistDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.basecommission.GetlistDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.currency.GetCurrencyRewardListDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.discount.DiscountListDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.reward.CreditRewardReturnListDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.reward.CurrencyRewardReturnListDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.reward.GetRewardListDto;
import com.ruicar.afs.cloud.bizcommon.commission.enums.InterfaceModeByUserId;
import com.ruicar.afs.cloud.bizcommon.commission.enums.InterfaceModeDown;
import com.ruicar.afs.cloud.bizcommon.commission.enums.InterfaceModeUpload;
import com.ruicar.afs.cloud.bizcommon.commission.feign.BasecommissionFeign;
import com.ruicar.afs.cloud.bizcommon.commission.feign.DiscountmainFeign;
import com.ruicar.afs.cloud.bizcommon.commission.feign.QuarterrewardFeign;
import com.ruicar.afs.cloud.bizcommon.commission.feign.RewardFeign;
import com.ruicar.afs.cloud.bizcommon.commission.service.CommissionService;
import feign.Response;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Description;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName CommissionServiceImpl
 * @Description: 佣金接口
 * @Author zyr
 * @Date 2020年6月9日 09:54:57
 * @Version V1.0
 **/
@Slf4j
@Service
@Component
@AllArgsConstructor
public class CommissionServiceImpl implements CommissionService {

    DiscountmainFeign discountmainFeign;
    RewardFeign rewardFeign;
    QuarterrewardFeign quarterrewardFeign;
    BasecommissionFeign basecommissionFeign;

    /**
     * @param file 文件
     * @param type     上传接口
     * @Description 文件上传接口专用
     */
    public JSONObject uploadFile(MultipartFile file, String type) {
        JSONObject json = new JSONObject();
        if (InterfaceModeUpload.basecommissionUpload.getValue().equals(type)) {
            json = basecommissionFeign.upload(file);
        } else if (InterfaceModeUpload.discountmainUploadImage.getValue().equals(type)) {
            json = discountmainFeign.uploadImage(file);
        }
        return json;
    }

    /**
     * @param id       文件ID
     * @param fileName 文件名称
     * @param type     下载接口
     * @return
     * @Description 文件下载接口专用
     */
    public ResponseEntity<byte[]> downFile(String id, String fileName, String type) {
        log.info("使用feign调用服务 文件下载");
        ResponseEntity<byte[]> result = null;
        InputStream inputStream = null;
        try {
            // feign文件下载
            Response response = null;
            if (InterfaceModeDown.rewardExport.getValue().equals(type)) {
                response = rewardFeign.export(id);
            } else if (InterfaceModeDown.rewardSignletter.getValue().equals(type)) {
                response = rewardFeign.signletter(id);
            } else if (InterfaceModeDown.discountmainExportRealse.getValue().equals(type)) {
                response = discountmainFeign.exportRealse(id);
            } else if (InterfaceModeDown.discountmainPdf.getValue().equals(type)) {
                response = discountmainFeign.pdf(id);
            } else if (InterfaceModeDown.quarterrewardSignletter.getValue().equals(type)) {
                response = quarterrewardFeign.signletter(id);
            } else if (InterfaceModeDown.basecommissionExport.getValue().equals(type)) {
                response = basecommissionFeign.export(id);
            } else if (InterfaceModeDown.basecommissionSignletter.getValue().equals(type)) {
                response = basecommissionFeign.signletter(id);
            } else if (InterfaceModeDown.basecommissionDownloadattachs.getValue().equals(type)) {
                response = basecommissionFeign.downloadattachs(id);
            }
            Response.Body body = response.body();
            inputStream = body.asInputStream();
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);
            HttpHeaders heads = new HttpHeaders();
            heads.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
            heads.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
            result = new ResponseEntity<byte[]>(b, heads, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * @param json   请求参数
     * @param userId cmp当前登录用户的id
     * @param type   接口方式
     * @return
     */
    public JSONObject getInfoByUserId(JSONObject json, String userId, String type) {
        //请求头
        Map headers = new HashMap<>();
        headers.put("userId", userId);
        //请求结果
        JSONObject result = new JSONObject();
        if (InterfaceModeByUserId.personcommissionGetlist.getValue().equals(type)) {
            result = quarterrewardFeign.getlist(json, headers);
        }
        return result;
    }

    @Description("基本服务费-下发信息列表")
    public JSONObject getlist(GetlistDto getlistDto, String userId,String sys) {
        return basecommissionFeign.getlist(getlistDto, makeHeaders(userId,sys));
    }
    @Description("基本服务费-下发信息列表")
    public JSONObject getDiscountList(DiscountListDto getlistDto, String userId,String sys) {
        return discountmainFeign.getlist(getlistDto, makeHeaders(userId,sys));
    }
    @Description("基本服务费-下发信息列表")
    public JSONObject attachBackCurrencyList(CurrencyRewardReturnListDto getlistDto, String userId,String sys) {
        return quarterrewardFeign.attachbacklist(getlistDto, makeHeaders(userId,sys));
    }
    @Description("基本服务费-下发信息列表")
    public JSONObject attachBackList(CreditRewardReturnListDto getlistDto, String userId,String sys) {
        return rewardFeign.attachbacklist(getlistDto, makeHeaders(userId,sys));
    }


    @Description("基本服务费-确认函退回列表")
    public JSONObject attachbacklist(AttachbacklistDto attachbacklistDto, String userId,String sys) {
        return basecommissionFeign.attachbacklist(attachbacklistDto, makeHeaders(userId,sys));
    }

    @Description("摊销奖励-下发信息列表")
    public JSONObject getRewordList(GetRewardListDto getRewardListDto, String userId,String sys) {
        return rewardFeign.getlist(getRewardListDto, makeHeaders(userId,sys));
    }

    @Description("通用奖励-下发信息列表")
    public JSONObject getCurrencyRewordList(GetCurrencyRewardListDto getCurrencyRewardListDto, String userId,String sys) {
        return quarterrewardFeign.list(getCurrencyRewardListDto, makeHeaders(userId,sys));
    }

    /**
     * 根据userId拼接请求头
     *
     * @param userId
     * @return
     */
    public Map makeHeaders(String userId,String sys) {
        //请求头
        Map headers = new HashMap<>();
        headers.put("userId", userId);
        headers.put("sys",sys);
        return headers;
    }


}
